package com.angel.controller;

import com.angel.bean.Employee;
import com.angel.bean.InMessage;
import com.angel.bean.IpList;
import com.angel.bean.dto.SocketEmployeeDto;
import com.angel.service.EmployeeService;
import com.angel.service.IpListService;
import com.angel.socket.WebSocketUsers;
import com.angel.utils.AdminUserUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Description :
 * InitVersion : 1.0
 * CreateTime :  2018/12/6 2:28
 *
 * @author : Rabit
 * @version 1.0
 */
@Controller
@RequestMapping("/msg")
public class OtherController {
    @Autowired
    private IpListService ipListService;
    @Autowired
    private EmployeeService employeeService;

    @RequestMapping("/loginhistory")
    public String getLoginLog(@RequestParam(value = "pn", defaultValue = "1") String pn, Model model){

        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(pn);
        } catch (Exception e) {

        }
        PageHelper.startPage(pageNo, 20);
        Employee employee = AdminUserUtils.getPrincipal().getEmployee();
        InMessage inMessage = new InMessage();
        inMessage.setFrom(employee.getEmId());
        List<IpList> list = ipListService.getAll();
        if (list != null) {
            PageInfo pageInfo = new PageInfo(list);
            model.addAttribute("pageInfo", pageInfo);
        }

        return "loginHistory";
    }

    @RequestMapping("/onlineUser")
    public String getOnlineUser( Model model){
        try {
            Thread.sleep(5);
        } catch (InterruptedException e) {

        }
        Map<String, SocketEmployeeDto> map =WebSocketUsers.eMap;
        List<SocketEmployeeDto> eids = new ArrayList<>();
        for (SocketEmployeeDto v: map.values()) {
            eids.add(v);
        }
        model.addAttribute("list", eids);
        return "onlineUser";
    }
}
